package com.css.dzj.dzjjcss.modules.resultregister.controller;
import com.alibaba.fastjson.JSON;
import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjjcss.common.audit.service.SysAuditService;
import com.css.dzj.dzjjcss.common.util.BeanUtil;
import com.css.dzj.dzjjcss.common.util.Constant;
import com.css.dzj.dzjjcss.common.util.R;
import com.css.dzj.dzjjcss.modules.resultregister.entity.ResultRegisterEntity;
import com.css.dzj.dzjjcss.modules.resultregister.service.ResultRegisterService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 监管结果登记Controller
 * @author xgdou
 * @date 2020\3\23
 */
@RestController
@RequestMapping(value = "/resultRegister")
public class ResultRegisterController {

    @Autowired
    private ResultRegisterService resultRegisterService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private SysAuditService sysAuditService;
    /**
     * 新增
     */
    @PostMapping("/save")
    public R save(@RequestBody ResultRegisterEntity resultRegisterEntity){
        String s="";
        try {
            if(StringUtils.isEmpty(resultRegisterEntity)) {
                return R.error("监管登记信息不能为空");
            }
            resultRegisterService.add(resultRegisterEntity);
            s = JSON.toJSONString(resultRegisterEntity);
            sysAuditService.saveSuccessAudit("保存监管登记成功", Constant.JGJGDJ, Constant.PT,Constant.ADD,s,request);
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(resultRegisterEntity);
            sysAuditService.saveFailAudit("保存监管登记失败",Constant.JGJGDJ,Constant.PT,Constant.ADD,s,e,request);
            return R.error(e.getMessage());
        }
    }
    /**
     * 查看详情
     */
    @GetMapping("/queryById")
    public R queryById(@RequestParam String id) {
        String s="";
        try {
            ResultRegisterEntity resultRegisterEntity = resultRegisterService.findById(id);
            s = JSON.toJSONString(resultRegisterEntity);
            sysAuditService.saveSuccessAudit("获取监管登记详情成功", Constant.JGJGDJ, Constant.PT,Constant.SELETE,s,request);
            return R.ok().put("data", resultRegisterEntity);
        } catch(Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(id);
            sysAuditService.saveFailAudit("获取监管登记详情失败",Constant.JGJGDJ,Constant.PT,Constant.SELETE,s,e,request);
            return R.error(e.getMessage());
        }
    }
    /**
     * 暂存
     */
    @PostMapping("/saveOnly")
    public R saveOnly(@RequestBody ResultRegisterEntity resultRegisterEntity){
        String s="";
        try {
            if(StringUtils.isEmpty(resultRegisterEntity)) {
                return R.error("监管登记信息不能为空");
            }
            resultRegisterService.addOnly(resultRegisterEntity);
            s = JSON.toJSONString(resultRegisterEntity);
            sysAuditService.saveSuccessAudit("暂存监管登记成功", Constant.JGJGDJ, Constant.PT,Constant.ZCADD,s,request);
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(resultRegisterEntity);
            sysAuditService.saveFailAudit("获取监管登记详情失败",Constant.JGJGDJ,Constant.PT,Constant.ZCADD,s,e,request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody ResultRegisterEntity resultRegisterEntity){
        String s="";
        try {
            ResultRegisterEntity old = resultRegisterService.findById(resultRegisterEntity.getId());
            BeanUtil.copyBeanNotEmptyToBean(resultRegisterEntity, old);
            old.setUpdateTime(new Date());
            resultRegisterService.update(old);
            s = JSON.toJSONString(resultRegisterEntity);
            sysAuditService.saveSuccessAudit("修改监管登记成功", Constant.JGJGDJ, Constant.PT,Constant.UPDATE,s,request);
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(resultRegisterEntity);
            sysAuditService.saveFailAudit("修改监管登记失败",Constant.JGJGDJ,Constant.PT,Constant.UPDATE,s,e,request);
            return R.error(e.getMessage());
        }
    }
    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(String id){
        String s="";
        try {
            if(resultRegisterService.delete(id)){
                s = JSON.toJSONString(id);
                sysAuditService.saveSuccessAudit("删除监管登记成功", Constant.JGJGDJ, Constant.PT,Constant.DEL,s,request);
                return R.ok("操作成功!");
            }else{
                return  R.error("操作失败!");
            }
        } catch(Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(id);
            sysAuditService.saveFailAudit("删除监管登记失败",Constant.JGJGDJ,Constant.PT,Constant.DEL,s,e,request);
            return R.error(e.getMessage());
        }
    }
    /**
     *多条件查询监管结果登记信息
     */
    @GetMapping("/publicityList")
    public R getXcbList(@RequestParam(name = "createUser",required = false) String createUser,
                        @RequestParam(name = "projectName",required = false) String projectName,
                        @RequestParam(name = "administrativeDivision",required = false) String administrativeDivision,
                        @RequestParam(name = "supervisionType",required = false) String supervisionType,
                        @RequestParam(name = "createUserUnit",required = false) String createUserUnit,
                        @RequestParam (name = "status",required = false)String status,
                        @RequestParam (name = "releaseStartTime",required = false)String releaseStartTime,
                        @RequestParam (name = "releaseEndTime",required = false)String releaseEndTime,
                        @RequestParam (name = "unitName",required = false)String unitName,
                        @RequestParam Integer pageSize, @RequestParam Integer pageNo) {
        String s="";
        try{
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<ResultRegisterEntity> supervisionResults = resultRegisterService.getSupervisionList(createUser,projectName,administrativeDivision,supervisionType,createUserUnit,status,releaseStartTime,releaseEndTime,unitName,pageRequest);
            s = JSON.toJSONString(supervisionResults);
            sysAuditService.saveSuccessAudit("查询监管登记成功", Constant.JGJGDJ, Constant.PT,Constant.PAGE,s,request);
            return R.ok().put("data", supervisionResults);
        }catch (Exception e){
            e.printStackTrace();
            sysAuditService.saveFailAudit("查询监管登记失败",Constant.JGJGDJ,Constant.PT,Constant.PAGE,s,e,request);
            return R.error(e.getMessage());
        }
    }
    /**
     * 撤回
     */
    @PostMapping("/recall")
    public R recall(@RequestParam String id,
                    @RequestParam String otherMsg){
        String s="";
        try {
            ResultRegisterEntity old = resultRegisterService.findById(id);
            old.setStatus("2");
            old.setUpdateTime(new Date());
            old.setOtherMsg(otherMsg);
            resultRegisterService.update(old);
            s = JSON.toJSONString(old);
            sysAuditService.saveSuccessAudit("撤回监管登记成功", Constant.JGJGDJ, Constant.PT,Constant.CH,s,request);
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("撤回监管登记失败",Constant.JGJGDJ,Constant.PT,Constant.CH,s,e,request);
            return R.error(e.getMessage());
        }
    }


    /**
     * 提交
     */
    @PostMapping("/release")
    public R release(@RequestParam String id){
        String s="";
        try {
            ResultRegisterEntity old = resultRegisterService.findById(id);
            old.setStatus("1");
            old.setSubmitTime(new Date());
            resultRegisterService.update(old);
            s = JSON.toJSONString(old);
            sysAuditService.saveSuccessAudit("提交监管登记成功", Constant.JGJGDJ, Constant.PT,Constant.TJ,s,request);
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("提交监管登记失败",Constant.JGJGDJ,Constant.PT,Constant.TJ,s,e,request);
            return R.error(e.getMessage());
        }
    }

}
